<?php

namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Redis;

class PagesController extends BaseController
{

    public function welcome()
    {
        return view('welcome',['token'=>redis::get('token'),'USERID'=>redis::get('USERID')]);
    }
    public function index()
    {
        return view('index');
    }

    public function login(Request $request)  {  
        if($_POST){
            $input = $request->only('name','password');

            $jwt_token = '';
            if(!$jwt_token = JWTAuth::attempt($input)){
                return response()->json([
                    'success'=>false,
                    'message'=>'invalid name or password'
                ],401);
            }
            
            Redis::set('token',$jwt_token);
            Redis::set('USERID',$request->input('name'));

            return response()->json([
                'success'=> true,
                'token'=>$jwt_token
            ]);

        }
        return  view('login');  
    }
}
